% Core Algorithm of Wormhole

% Objective Definitions
% Nebulas and the locations inside Wormhole
Nebula= [2,3,1;4,1,8;6,3,5;1,2,1;2,2,2;5,1,7];
Nebula_size= 6;

% Post and Galaxy with the number of related Nebulas
post_or_galaxy= rand (1,1);
no_post= 0;
no_galaxy= 0;
no_tag= 3; % no. of the tagged Nebulas of a P/G, Max. no. is 4
tag_pointer= [0,0,1,1,1,0]; % generate the tags
% to determine whether it is a P or G
if post_or_galaxy > 0.2
    no_galaxy= no_galaxy+ 1;
else
    no_post= no_post+ 1;
end
% User (commentor and commentee) & Professional Values 
% indicates the tag status
Nebula_X_tag= [0,1,0,1,1,0]; %
Nebula_Y_tag= zeros (1,Nebula_size);
% PV records the professional values
Nebula_X_PV= [250,3000,2000,10000,7000,10];
Nebula_Y_PV= ones (1,Nebula_size);
Nebula_Y_PVeff= zeros (1,Nebula_size);
R= zeros (1, Nebula_size); % to record the relativity
ratio= zeros (1,Nebula_size); % the ration between Mi and Mo
m= 1;  % the professional weight of a G/P
m_initial= 1;  % the initial professional weight of a G/P
ratio= ones (1,Nebula_size); % the ration between X and Y
deltam= 0;
tag_threshold= 0;
tag_counter= 1;
posi_counter=0; % to track the positive comments
nega_counter=0; % to track the negative comments
k=5; % gamma distribution parameters
thida=1;

% Algorithm
num= 1000; % the number of commentors
user_counter= 1; % to track the number of commentors
distance_counter= 1; % to track the distance between Y and P/G

for user_counter= 1: num
    % Define each commentor
    for tag_counter= 1: Nebula_size
        Nebula_Y_PV(1,tag_counter)= 1000* gamrnd(k,thida); % gamma distribution with shape parameter of k and scale parameter of thida
        tag_threshold= rand(1,1);
        if tag_threshold >= 0.5
            Nebula_Y_tag(1,tag_counter)= 1;
        else
            Nebula_Y_tag(1,tag_counter)= 0;
        end
    end
    
    % Generate random 'lits' and 'dims';
    threshold= rand (1,1);
    if threshold >= 0.7
        posi_counter= posi_counter+1;
        p_n= 1;
    else nega_counter= nega_counter+1;
        p_n= -1;
    end
    
    % determine the difference between the positive and the negative comments
    dif_comment= posi_counter-nega_counter;
    % calculate deltam after each comment
    if dif_comment < 100 || dif_comment > -100
        deltam= 0.0025*pi*sin(pi*abs(dif_comment)/200);
    elseif dif_comment >= 100 || dif_comment < -100
        deltam= 0.005*exp(-0.01*(abs(dif_comment)-100));
    end
    
    % calculate the m according to the deltam
    m= m+p_n*deltam;
    
    % Determine the relativity of each tagged Nebulas of the P/G
    for tag_counter= 1: Nebula_size
        if tag_pointer(1,tag_counter) == 1 && Nebula_Y_tag(1,tag_counter) == 1
            R(1,tag_counter)= 1;
            Nebula_Y_PVeff(1,tag_counter)= Nebula_Y_PV(1,tag_counter);
        elseif tag_pointer(1,tag_counter) == 1 && Nebula_Y_tag(1,tag_counter) == 0
            Distance= zeros (1, Nebula_size);
            min_distance= 0; % to find the closest Nebula of Y
            for distance_counter= 1: Nebula_size
                if Nebula_Y_tag(1,distance_counter) == 1
                    Distance(1,distance_counter)= norm (Nebula(tag_counter,:)-Nebula(distance_counter,:));
                else Distance(1,distance_counter)= 10000;
                end
                if distance_counter == 1
                    min_distance= Distance(1,1);
                    Nebula_Y_PVeff(1,tag_counter)= Nebula_Y_PV(1,distance_counter);
                else
                    if Distance(1,distance_counter) < Distance(1,distance_counter-1)
                        min_distance= Distance(1, distance_counter);
                        Nebula_Y_PVeff(1,tag_counter)= Nebula_Y_PV(1,distance_counter);
                    end
                end
            end
            R(1,tag_counter)= exp(-min_distance);
        end
    end
    
    % Determine the effective PV ratio between X and Y
    for tag_counter= 1: Nebula_size
        if Nebula_Y_PVeff(1,tag_counter)== 0
            ratio(1,tag_counter)= 0;
        else
            ratio(1,tag_counter)= Nebula_Y_PVeff(1,tag_counter)/Nebula_X_PV(1,tag_counter);
        end
        
        % ratio correction
        if ratio(1,tag_counter) >= 2
            ratio(1,tag_counter)= 3-exp(2-ratio(1,tag_counter)); % with 3 to be the upper limit of the ratio
        elseif ratio(1,tag_counter) <= 0.5
            ratio(1,tag_counter)= 8*ratio(1,tag_counter)^4;
        else
            ratio(1,tag_counter)= ratio(1,tag_counter);
        end
    end
    
    % Final Calculation
    for tag_counter= 1: Nebula_size
        if tag_pointer(1,tag_counter) == 1 && Nebula_X_tag(1,tag_counter) == 1
            Nebula_X_PV(1,tag_counter)= ratio(1,tag_counter)* m* p_n* R(1,tag_counter)+ Nebula_X_PV(1,tag_counter);
        elseif tag_pointer(1,tag_counter) == 1 && Nebula_X_tag(1,tag_counter) == 0
            Nebula_X_PV(1,tag_counter)=  0.5*ratio(1,tag_counter)* m* p_n* R(1,tag_counter)+ Nebula_X_PV(1,tag_counter);
        end
        
        if Nebula_X_PV(1,tag_counter) <= 1
            Nebula_X_PV(1,tag_counter)=1;
        end
    end
end
t= [1:1:num];
plot(m,t);
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    